package com.android.support.lib.universal.view.dialog;

import com.android.support.lib.universal.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 自定义加载进度
 * <p/>
 * CustomProgressDialog.createDialog(this)//初始化 progressDialog.show();//显示 if
 * (progressDialog.isShowing()) { progressDialog.dismiss();//关闭 }
 *
 * @author 彭治铭
 */
public class CustomProgressDialog extends Dialog {

    private static CustomProgressDialog customProgressDialog = null;
    @SuppressWarnings("unused")
    private static Activity scontext = null;

    public CustomProgressDialog(Activity context) {
        super(context);
        this.scontext = context;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    /**
     * CustomProgressDialog对象不能是静态的。因为 context没有静态的Activity上下文。
     *
     * @param context 必须到当前Activity的上下文。BaseApplication.getInstance()会报错。
     * @return
     */
    public static CustomProgressDialog createDialog(Context context) {
        scontext = (Activity) context;
        customProgressDialog = new CustomProgressDialog(context,
                R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.lib_customprogressdialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        customProgressDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        return customProgressDialog;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
        TextView tvMsg = (TextView) customProgressDialog
                .findViewById(R.id.id_tv_loadingmsg);
        AnimationDrawable animationDrawable = (AnimationDrawable) tvMsg.getCompoundDrawables()[1];
        animationDrawable.start();
    }

    //给进度条添加显示内容
    public CustomProgressDialog setMessage(String strMessage) {
        TextView tvMsg = (TextView) customProgressDialog
                .findViewById(R.id.id_tv_loadingmsg);
        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }
        return customProgressDialog;
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub
        try {
            if (scontext != null) {
                if (customProgressDialog != null && !scontext.isFinishing()) {
                    if (!isShowing()) {
                        super.show();
                    } else {
                        dismiss();
                        show();
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("test", "CustomProgressDialog显示出错" + e.getMessage());
        }
    }

    @Override
    public void dismiss() {
        // TODO Auto-generated method stub
        try {
            if (scontext != null) {
                if (customProgressDialog != null && !scontext.isFinishing()) {
                    if (isShowing()) {
                        super.dismiss();
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("test", "CustomProgressDialog关闭出错" + e.getMessage());
        }

    }

}
